package main

import "fmt"

func combinationSum(candidates []int, target int) [][]int {
	// 双指针
	candidates = append(candidates, 0)
	n := len(candidates)
	left, right := 0, 0
	result := make([][]int, 0)
	result = append(result, []int{})
	for {
		tempArr := candidates[left:right + 1]
		//if flag := add(tempArr, target); flag {
			result = append(result, tempArr)
		// }
		right ++
		if right == n - 1 {
			left ++
			right = left
		}

		// 退出循环
		if left == n - 1 && right == n - 1 {
			break
		}
	}
	return result
}

// 计算总和
//func add(arr []int, target int) bool {
//	sum := 0
//	for _, v := range arr {
//		sum += v
//	}
//	if sum == target {
//		return true
//	}
//	return false
//}

func main() {
	fmt.Println(combinationSum([]int{1, 2, 3}, 7))
}
